package cn.doitedu.demo.connectors;

import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Jdbc连接器_写 {
    public static void main(String[] args) {
        // 构造stream api 编程的环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(5000, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointStorage("file:///d:/ckpt");
        env.setParallelism(1);

        // 构造 sql 编程环境
        StreamTableEnvironment tenv = StreamTableEnvironment.create(env);


        // 创建数据源映射表
        tenv.executeSql("CREATE TABLE t1                  (\n" +
                "  uid BIGINT,                               \n" +
                "  event_id STRING,                          \n" +
                "  properties MAP<STRING,STRING>,            \n" +
                "  action_time BIGINT,                       \n" +

                "  rt as to_timestamp_ltz(action_time,3),    \n" +
                "  watermark for rt as rt                    \n" +
                ") WITH (                                    \n" +
                "  'connector' = 'kafka',                    \n" +
                "  'topic' = 'ss-1',                         \n" +
                "  'properties.bootstrap.servers' = 'doitedu:9092',\n" +
                "  'properties.group.id' = 'doit44_g1',      \n" +
                "  'scan.startup.mode' = 'latest-offset',    \n" +
                "  'value.format' = 'json',                  \n" +
                "  'value.fields-include' = 'EXCEPT_KEY'     \n" +
                ")");


        // 创建目标数据源映射表，映射mysql中的表
        tenv.executeSql("CREATE TABLE t2                 ( \n" +
                "  url STRING,                               \n" +
                "  pv  BIGINT,                               \n" +
                "  primary key(url) not enforced             \n" +
                ") WITH (                                    \n" +
                "   'connector' = 'jdbc',                    \n" +
                "   'url' = 'jdbc:mysql://doitedu:3306/flinktest',\n" +
                "   'table-name' = 'jdbc_test',                   \n" +
                "   'username' = 'root',                      \n" +
                "   'password' = 'root'                       \n" +
                ");");

        // 普通groupby聚合
        tenv.executeSql(
                "INSERT INTO t2             \n" +
                 "SELECT                      \n" +
                "    properties['url'] as url,\n" +
                "    count(1) as pv           \n" +
                "FROM t1                      \n" +
                "GROUP BY                     \n" +
                "    properties['url'] ").print();







    }
}
